package com.wifipositioner.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wifipositioner.Globals;
import com.wifipositioner.R;
import com.wifipositioner.measure.MeasuredPoint;
import com.wifipositioner.measure.MeasuredPointList;
import com.wifipositioner.positioning.RefPointCache;
import com.wifipositioner.wifi.WifiProvider;

public class MeasureActivity extends Activity{
	
	private Button btn_start;
	private ProgressBar progressBar;
	private TextView tv_refName;
	private String fileNameToWrite = "";
	private Handler wifiHandler;
	private MeasuredPoint measuredRefPoint;
	private MeasuredPointList measuredRefPointList;
	private int measureCntr = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.measure);
		RefPointCache.get().clearCache();
		btn_start = (Button) findViewById(R.id.measure_btn_start);
		progressBar = (ProgressBar) findViewById(R.id.measure_progressbar);
		progressBar.setMax(Globals.maxMeasureCntr);
		tv_refName = (TextView) findViewById(R.id.measure_tv_refName);
		measuredRefPointList = new MeasuredPointList();
		Bundle extras = getIntent().getExtras(); 
		if(extras != null)
		{
			fileNameToWrite = extras.getString(Globals.referenceFileNameExtra);
		}
		super.onCreate(savedInstanceState);
	}
	private void registerHandlers(){
		btn_start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startMeasure();
			}
			
		});
		tv_refName.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final AlertDialog.Builder alert = new AlertDialog.Builder(MeasureActivity.this);
				final EditText input = new EditText(MeasureActivity.this);
				alert.setView(input);
				alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						tv_refName.setText(input.getText());
					}
				});

				alert.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int whichButton) {
								dialog.cancel();
							}
						});
				alert.show();
			}
		});
		wifiHandler = new Handler(new Callback() {
			
			@Override
			public boolean handleMessage(Message msg) {
				progressBar.incrementProgressBy(1);
				if(measureCntr < Globals.maxMeasureCntr){
					WifiProvider.get().scan();
				}
				else{
					measureStopped();
				}
				List<ScanResult> results = (List<ScanResult>) msg.obj;
				measuredRefPoint.add(results);
				measureCntr++;
				return false;
			}
		});
		WifiProvider.get().registerHandler(wifiHandler);
	}
	private void measureStopped() {
		btn_start.setEnabled(true);
		progressBar.setProgress(0);
		
	}
	private void storeMesureResult() {
		
		try {
			RefPointCache.get().saveCacheToFile(measuredRefPointList, fileNameToWrite);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void startMeasure() {
		btn_start.setEnabled(false);
		measuredRefPoint = new MeasuredPoint(tv_refName.getText().toString());
		measuredRefPointList.add(measuredRefPoint);
		measureCntr = 1;
		WifiProvider.get().scan();
	}
	
	@Override
	protected void onStart() {
		registerHandlers();
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		WifiProvider.get().removeHandler(wifiHandler);
		super.onStop();
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
	       case 0: {
	    	   storeMesureResult();
	    	   finish();
	    	   break;
	       }	   
	
 	}
		return super.onMenuItemSelected(featureId, item);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE,0,0,"Save");
		return super.onCreateOptionsMenu(menu);
	}

}
